1
|
|
|
/*jslint |
2
|
|
|
indent: 4 |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
/*global |
6
|
|
|
$, google, mytrans, window, Cookies, Coordinates, get_cookie_string |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
var Okapi = {}; |
11
|
|
|
Okapi.m_map = null; |
12
|
|
|
Okapi.m_sites = null; |
13
|
|
|
Okapi.m_ready = false; |
14
|
|
|
Okapi.m_showCache = null; |
15
|
|
|
Okapi.m_enabled = false; |
16
|
|
|
Okapi.m_popup = null; |
17
|
|
|
Okapi.m_marker = null; |
18
|
|
|
Okapi.m_icons = null; |
19
|
|
|
Okapi.m_timer = null; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
Okapi.init = function (themap) { |
23
|
|
|
'use strict'; |
24
|
|
|
|
25
|
|
|
this.m_map = themap; |
26
|
|
|
var self = this; |
27
|
|
|
|
28
|
|
|
google.maps.event.addListener(this.m_map, "center_changed", function () { |
29
|
|
|
self.scheduleLoad(); |
30
|
|
|
}); |
31
|
|
|
google.maps.event.addListener(this.m_map, "zoom_changed", function () { |
32
|
|
|
self.scheduleLoad(); |
33
|
|
|
}); |
34
|
|
|
}; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
Okapi.parseLocation = function (s) { |
38
|
|
|
'use strict'; |
39
|
|
|
|
40
|
|
|
var loc = s.split("|"), |
41
|
|
|
lat = parseFloat(loc[0]), |
42
|
|
|
lng = parseFloat(loc[1]); |
43
|
|
|
|
44
|
|
|
if (Coordinates.valid(lat, lng)) { |
45
|
|
|
return new google.maps.LatLng(lat, lng); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return null; |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
Okapi.setShowCache = function (code) { |
53
|
|
|
'use strict'; |
54
|
|
|
|
55
|
|
|
this.m_showCache = code; |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
Okapi.setupSites = function () { |
60
|
|
|
'use strict'; |
61
|
|
|
|
62
|
|
|
if (this.m_sites) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
var self = this, |
67
|
|
|
main_url = "http://www.opencaching.pl/okapi/services/apisrv/installations", |
68
|
|
|
keys = { |
69
|
|
|
"Opencaching.DE" : "YSqPufH82encfJ67ZxV2", |
70
|
|
|
"Opencaching.PL" : "jhRyc6rGmT6XEvxva29B", |
71
|
|
|
"Opencaching.NL" : "gcwaesuq3REu8RtCgLDj", |
72
|
|
|
"Opencaching.US" : "GvgyCMvwfH42GqJGL494", |
73
|
|
|
"Opencaching.ORG.UK" : "7t7VfpkCd4HuxPabfbHd", |
74
|
|
|
"Opencaching.RO" : "gqSWmVJhZGDwc4sRhyy7" |
75
|
|
|
}, |
76
|
|
|
prefixes = { |
77
|
|
|
"Opencaching.DE" : "OC", |
78
|
|
|
"Opencaching.PL" : "OP", |
79
|
|
|
"Opencaching.NL" : "OB", |
80
|
|
|
"Opencaching.US" : "OU", |
81
|
|
|
"Opencaching.ORG.UK" : "OK", |
82
|
|
|
"Opencaching.RO" : "OR" |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
this.m_sites = []; |
86
|
|
|
|
87
|
|
|
$.ajax({ |
88
|
|
|
url: main_url, |
89
|
|
|
dataType: 'json', |
90
|
|
|
success: function (response) { |
91
|
|
|
response.map(function (site) { |
92
|
|
|
if (keys[site.site_name] !== undefined) { |
93
|
|
|
//console.log("adding OC site: " + site.site_name); |
94
|
|
|
self.m_sites.push({ |
95
|
|
|
siteid: self.m_sites.length, |
96
|
|
|
name: site.site_name, |
97
|
|
|
site_url: site.site_url, |
98
|
|
|
url: site.okapi_base_url, |
99
|
|
|
prefix: prefixes[site.site_name], |
100
|
|
|
key: keys[site.site_name], |
101
|
|
|
ignore_user: null, |
102
|
|
|
markers: {}, |
103
|
|
|
finished: true |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
self.m_ready = true; |
109
|
|
|
if (self.m_enabled) { |
110
|
|
|
self.scheduleLoad(true); |
111
|
|
|
} |
112
|
|
|
if (self.m_showCache && self.m_showCache !== "") { |
113
|
|
|
self.centerMap(self.m_showCache); |
114
|
|
|
self.m_showCache = null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
}); |
118
|
|
|
}; |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
Okapi.setupIcons = function () { |
122
|
|
|
'use strict'; |
123
|
|
|
|
124
|
|
|
if (this.m_icons) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
this.m_icons = { |
129
|
|
|
"Other": new google.maps.MarkerImage("img/cachetype-1.png"), |
130
|
|
|
"Traditional": new google.maps.MarkerImage("img/cachetype-2.png"), |
131
|
|
|
"Multi": new google.maps.MarkerImage("img/cachetype-3.png"), |
132
|
|
|
"Virtual": new google.maps.MarkerImage("img/cachetype-4.png"), |
133
|
|
|
"Webcam": new google.maps.MarkerImage("img/cachetype-5.png"), |
134
|
|
|
"Event": new google.maps.MarkerImage("img/cachetype-6.png"), |
135
|
|
|
"Quiz": new google.maps.MarkerImage("img/cachetype-7.png"), |
136
|
|
|
"Math/Physics": new google.maps.MarkerImage("img/cachetype-8.png"), |
137
|
|
|
"Moving": new google.maps.MarkerImage("img/cachetype-9.png"), |
138
|
|
|
"Drive-In": new google.maps.MarkerImage("img/cachetype-10.png") |
139
|
|
|
}; |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
Okapi.icon = function (type) { |
144
|
|
|
'use strict'; |
145
|
|
|
|
146
|
|
|
if (this.m_icons[type] !== undefined) { |
147
|
|
|
return this.m_icons[type]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return this.m_icons.Other; |
151
|
|
|
}; |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
Okapi.guessSiteId = function (code) { |
155
|
|
|
'use strict'; |
156
|
|
|
|
157
|
|
|
code = code.toUpperCase(); |
158
|
|
|
var siteid; |
159
|
|
|
for (siteid = 0; siteid < this.m_sites.length; siteid += 1) { |
160
|
|
|
if (code.startsWith(this.m_sites[siteid].prefix)) { |
161
|
|
|
return siteid; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return -1; |
166
|
|
|
}; |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
Okapi.centerMap = function (code) { |
170
|
|
|
'use strict'; |
171
|
|
|
|
172
|
|
|
if (!this.m_ready) { |
173
|
|
|
//console.log("okapi not ready"); |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
var siteid = this.guessSiteId(code); |
178
|
|
|
if (siteid < 0) { |
179
|
|
|
//console.log("bad code. cannot determine okapi site"); |
180
|
|
|
return; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
this.showPopup(null, code.toUpperCase(), siteid); |
184
|
|
|
}; |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
Okapi.createPopupContent = function (code, response) { |
188
|
|
|
'use strict'; |
189
|
|
|
|
190
|
|
|
var content = |
191
|
|
|
'<a href="' + response.url + '" target="_blank">' + code + ' <b>' + response.name + '</b></a><br />' |
192
|
|
|
+ '<table>' |
193
|
|
|
+ '<tr><td>' + mytrans("geocache.owner") + '</td><td>' + '<a href="' + response.owner.profile_url + '" target="_blank"><b>' + response.owner.username + '</b></a></td></tr>' |
194
|
|
|
+ '<tr><td>' + mytrans("geocache.type") + '</td><td>' + response.type + '</td></tr>' |
195
|
|
|
+ '<tr><td>' + mytrans("geocache.size") + '</td><td>' + response.size2 + '</td></tr>' |
196
|
|
|
+ '<tr><td>' + mytrans("geocache.status") + '</td><td>' + response.status + '</td></tr>' |
197
|
|
|
+ '<tr><td>' + mytrans("geocache.difficulty") + '</td><td>' + response.difficulty + '/5</td></tr>' |
198
|
|
|
+ '<tr><td>' + mytrans("geocache.terrain") + '</td><td>' + response.terrain + '/5</td></tr>' |
199
|
|
|
+ '<tr><td>' + mytrans("geocache.finds") + '</td><td>' + response.founds + '</td></tr>' |
200
|
|
|
+ '</table>'; |
201
|
|
|
return content; |
202
|
|
|
}; |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
Okapi.showPopup = function (m, code, siteid) { |
206
|
|
|
'use strict'; |
207
|
|
|
|
208
|
|
|
if (!this.m_popup) { |
209
|
|
|
this.m_popup = new google.maps.InfoWindow(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
var self = this, |
213
|
|
|
site = this.m_sites[siteid]; |
214
|
|
|
|
215
|
|
|
$.ajax({ |
216
|
|
|
url: site.url + 'services/caches/geocache', |
217
|
|
|
dataType: 'json', |
218
|
|
|
data: { |
219
|
|
|
'consumer_key': site.key, |
220
|
|
|
'cache_code': code, |
221
|
|
|
'fields' : 'name|type|status|url|owner|founds|size2|difficulty|terrain|location' |
222
|
|
|
}, |
223
|
|
|
success: function (response) { |
224
|
|
|
var coords = self.parseLocation(response.location); |
225
|
|
|
self.m_map.setCenter(coords); |
226
|
|
|
|
227
|
|
|
if (!m) { |
228
|
|
|
m = new google.maps.Marker({ |
229
|
|
|
position: coords, |
230
|
|
|
map: self.m_map, |
231
|
|
|
icon: self.icon(response.type) |
232
|
|
|
}); |
233
|
|
|
if (self.m_maker) { |
234
|
|
|
self.m_marker.setMap(null); |
235
|
|
|
} |
236
|
|
|
self.registerPopup(m, code, siteid); |
237
|
|
|
self.m_marker = m; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
self.m_popup.setContent(self.createPopupContent(code, response)); |
241
|
|
|
self.m_popup.open(self.m_map, m); |
242
|
|
|
} |
243
|
|
|
}); |
244
|
|
|
}; |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
Okapi.registerPopup = function (m, code, siteid) { |
248
|
|
|
'use strict'; |
249
|
|
|
|
250
|
|
|
if (!this.m_ready) { |
251
|
|
|
return; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
var self = this; |
255
|
|
|
|
256
|
|
|
google.maps.event.addListener(m, 'click', function () { |
257
|
|
|
self.showPopup(m, code, siteid); |
258
|
|
|
}); |
259
|
|
|
}; |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
Okapi.removeMarkersSite = function (markers) { |
263
|
|
|
'use strict'; |
264
|
|
|
|
265
|
|
|
if (!this.m_ready) { |
266
|
|
|
return; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if (markers) { |
270
|
|
|
markers.map(function (m) { |
271
|
|
|
m.setMap(null); |
272
|
|
|
}); |
273
|
|
|
} |
274
|
|
|
}; |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
Okapi.removeMarkers = function () { |
278
|
|
|
'use strict'; |
279
|
|
|
|
280
|
|
|
if (!this.m_ready) { |
281
|
|
|
return; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
var self = this; |
285
|
|
|
this.m_sites.map(function (site) { |
286
|
|
|
self.removeMarkersSite(site.markers); |
287
|
|
|
site.markers = {}; |
288
|
|
|
}); |
289
|
|
|
|
290
|
|
|
if (this.m_marker) { |
291
|
|
|
this.m_marker.setMap(null); |
292
|
|
|
delete this.m_marker; |
293
|
|
|
this.m_marker = null; |
294
|
|
|
} |
295
|
|
|
}; |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
Okapi.loadBboxSite = function (siteid) { |
299
|
|
|
'use strict'; |
300
|
|
|
|
301
|
|
|
if (!this.m_ready) { |
302
|
|
|
return; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
var self = this, |
306
|
|
|
site = this.m_sites[siteid], |
307
|
|
|
b, |
308
|
|
|
bbox; |
309
|
|
|
|
310
|
|
|
if (!this.m_enabled) { |
311
|
|
|
site.finished = true; |
312
|
|
|
return; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
if (!site.finished) { |
316
|
|
|
return; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
site.finished = false; |
320
|
|
|
|
321
|
|
|
b = this.m_map.getBounds(); |
322
|
|
|
bbox = b.getSouthWest().lat() + "|" + b.getSouthWest().lng() + "|" + b.getNorthEast().lat() + "|" + b.getNorthEast().lng(); |
323
|
|
|
|
324
|
|
|
$.ajax({ |
325
|
|
|
url: site.url + 'services/caches/shortcuts/search_and_retrieve', |
326
|
|
|
dataType: 'json', |
327
|
|
|
data: { |
328
|
|
|
'consumer_key': site.key, |
329
|
|
|
'search_method': 'services/caches/search/bbox', |
330
|
|
|
'search_params': '{"bbox" : "' + bbox + '", "limit" : "500"}', |
331
|
|
|
'retr_method': 'services/caches/geocaches', |
332
|
|
|
'retr_params': '{"fields": "code|name|location|type|status|url"}', |
333
|
|
|
'wrap': 'false' |
334
|
|
|
}, |
335
|
|
|
success: function (response) { |
336
|
|
|
var addedCaches = {}, |
337
|
|
|
cache, |
338
|
|
|
code; |
339
|
|
|
|
340
|
|
|
for (code in response) { |
341
|
|
|
if (!response.hasOwnProperty(code)) { |
342
|
|
|
continue; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
cache = response[code]; |
346
|
|
|
if (cache.status !== "Available") { |
347
|
|
|
continue; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
addedCaches[cache.code] = true; |
351
|
|
|
if (site.markers[cache.code] !== undefined) { |
352
|
|
|
continue; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
site.markers[cache.code] = new google.maps.Marker({ |
356
|
|
|
position: self.parseLocation(cache.location), |
357
|
|
|
map: self.m_map, |
358
|
|
|
icon: self.icon(cache.type) |
359
|
|
|
}); |
360
|
|
|
|
361
|
|
|
self.registerPopup(site.markers[cache.code], cache.code, siteid); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
for (code in site.markers) { |
365
|
|
|
if (site.markers.hasOwnProperty(code) && addedCaches[code] === undefined) { |
366
|
|
|
site.markers[code].setMap(null); |
367
|
|
|
delete site.markers[code]; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
site.finished = true; |
371
|
|
|
}, |
372
|
|
|
error: function () { |
373
|
|
|
//console.log("okapi request failed: " + site.name); |
374
|
|
|
self.removeMarkersSite(site.markers); |
375
|
|
|
site.markers = {}; |
376
|
|
|
site.finished = true; |
377
|
|
|
} |
378
|
|
|
}); |
379
|
|
|
}; |
380
|
|
|
|
381
|
|
|
|
382
|
|
|
Okapi.loadBbox = function () { |
383
|
|
|
'use strict'; |
384
|
|
|
|
385
|
|
|
if (!this.m_ready) { |
386
|
|
|
return; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
var self = this; |
390
|
|
|
this.m_sites.map(function (site) { |
391
|
|
|
self.loadBboxSite(site.siteid); |
392
|
|
|
}); |
393
|
|
|
}; |
394
|
|
|
|
395
|
|
|
|
396
|
|
|
Okapi.unscheduleLoad = function () { |
397
|
|
|
'use strict'; |
398
|
|
|
|
399
|
|
|
if (!this.m_ready) { |
400
|
|
|
return; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
if (this.m_timer) { |
404
|
|
|
window.clearTimeout(this.m_timer); |
405
|
|
|
this.m_timer = null; |
406
|
|
|
} |
407
|
|
|
}; |
408
|
|
|
|
409
|
|
|
|
410
|
|
|
Okapi.scheduleLoad = function () { |
411
|
|
|
'use strict'; |
412
|
|
|
|
413
|
|
|
if (!this.m_ready) { |
414
|
|
|
return; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
var self = this; |
418
|
|
|
|
419
|
|
|
this.unscheduleLoad(); |
420
|
|
|
this.m_timer = window.setTimeout(function () { |
421
|
|
|
self.loadBbox(); |
422
|
|
|
}, 500); |
423
|
|
|
}; |
424
|
|
|
|
425
|
|
|
|
426
|
|
|
Okapi.toggle = function (t) { |
427
|
|
|
'use strict'; |
428
|
|
|
|
429
|
|
|
Cookies.set('load_caches', t ? "1" : "0", {expires: 30}); |
430
|
|
|
if ($('#geocaches').is(':checked') !== t) { |
431
|
|
|
$('#geocaches').attr('checked', t); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
if (this.m_enabled !== t) { |
435
|
|
|
this.m_enabled = t; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
if (this.m_enabled) { |
439
|
|
|
this.setupIcons(); |
440
|
|
|
this.setupSites(); |
441
|
|
|
this.scheduleLoad(); |
442
|
|
|
} else { |
443
|
|
|
this.unscheduleLoad(); |
444
|
|
|
this.removeMarkers(); |
445
|
|
|
} |
446
|
|
|
}; |
447
|
|
|
|
448
|
|
|
|
449
|
|
|
Okapi.restore = function (defaultValue) { |
450
|
|
|
'use strict'; |
451
|
|
|
|
452
|
|
|
var state = get_cookie_string("load_caches", "invalid"); |
453
|
|
|
|
454
|
|
|
if (state === "0") { |
455
|
|
|
this.toggle(false); |
456
|
|
|
} else if (state === "1") { |
457
|
|
|
this.toggle(true); |
458
|
|
|
} else { |
459
|
|
|
this.toggle(defaultValue); |
460
|
|
|
} |
461
|
|
|
}; |
462
|
|
|
|